﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;
using XmlVisualizer.Models;

namespace XmlVisualizer.Dialogs {
	/// <summary>
	/// Interaction logic for DialogAddNewTextNode.xaml
	/// </summary>
	public partial class DialogAddNewTextNode : Window {
		#region Fields
		bool posAdjusted;
		private XNodeInfo newNode = null;
		#endregion

		#region Constructors
		/// <summary>
		/// Initializes a new instance of the DialogAddNewTextNode.
		/// </summary>
		public DialogAddNewTextNode() {
			InitializeComponent();
		}
		#endregion

		#region Methods
		/// <summary>
		/// Displays and shows the window until user closes it.
		/// </summary>
		/// <returns><see cref="DialogAddElementAttribute.IDOK">IDOK</see> on successful closure,
		/// <see cref="DialogAddElementAttribute.IDCANCEL">IDCANCEL</see> otherwise.</returns>
		public XNodeInfo ShowModal() {
			ShowDialog(); // this "blocks" until window is closed
			return newNode;
		}
		#endregion

		#region Event handlers
		private void Button_Click(object sender, RoutedEventArgs e) {
			string textString = tb_Text.Text;
			XText textNode = new XText(textString);
			XNodeInfo info = new XNodeInfo(new Point(0, 0), 0, 0, "", textNode);
			newNode = info;
			this.Close();
		}

		/// <summary>
		/// Invoked when window gets activated.
		/// </summary>
		/// <param name="e">The event data.</param>
		protected override void OnActivated(EventArgs e) {
			base.OnActivated(e);
			if (!posAdjusted) {
				this.Left -= this.ActualWidth;
				this.Top -= this.ActualHeight;
				posAdjusted = true;
			}

			tb_Text.Focus();
			tb_Text.SelectAll();
		}
		#endregion		
	}
}
